package dao

import (
	"wxcloudrun-golang/db"
	"wxcloudrun-golang/db/model"
)

const tableName = "Counters"
const agency_table = "agency_transfer"

// ClearCounter 清除Counter
func (imp *CounterInterfaceImp) ClearCounter(id int32) error {
	cli := db.Get()
	return cli.Table(tableName).Delete(&model.CounterModel{Id: id}).Error
}

// UpsertCounter 更新/写入counter
func (imp *CounterInterfaceImp) UpsertCounter(counter *model.CounterModel) error {
	cli := db.Get()
	return cli.Table(tableName).Save(counter).Error
}

// GetCounter 查询Counter
func (imp *CounterInterfaceImp) GetCounter(id int32) (*model.CounterModel, error) {
	var err error
	var counter = new(model.CounterModel)

	cli := db.Get()
	err = cli.Table(tableName).Where("id = ?", id).First(counter).Error

	return counter, err
}

func (imp *AgencyInterfaceImp) ClearAgency(id int64) error {
	cli := db.Get()
	return cli.Table(agency_table).Delete(&model.AgencyTransfer{ID: id}).Error
}

// UpsertCounter 更新/写入counter
func (imp *AgencyInterfaceImp) UpsertAgency(agency *model.AgencyTransfer) error {
	cli := db.Get()
	return cli.Table(agency_table).Save(agency).Error
}

// GetCounter 查询Counter
func (imp *AgencyInterfaceImp) GetAgency(id int64) (*model.AgencyTransfer, error) {
	var err error
	var agency = new(model.AgencyTransfer)

	cli := db.Get()
	err = cli.Table(agency_table).Where("id = ?", id).First(agency).Error

	return agency, err
}

func (imp *AgencyInterfaceImp) ListByType(page, pageSize int) ([]model.AgencyTransfer, int64, error) {
	var err error
	var agencys []model.AgencyTransfer
	var total int64

	cli := db.Get()
	cli.Table(agency_table).Count(&total)

	offset := page * pageSize
	err = cli.Table(agency_table).Offset(offset).Limit(pageSize).Order("create_at DESC").Find(&agencys).Error
	return agencys, total, err
}

// 创建信息（带扩展数据）
// func (dao *informationDAO) CreateWithExtension(ctx context.Context, info *Information, ext interface{}) error {
//     return dao.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
//         // 1. 创建主信息
//         if err := tx.Create(info).Error; err != nil {
//             return err
//         }

//         // 2. 使用反射设置扩展模型ID
//         extValue := reflect.ValueOf(ext).Elem()
//         extValue.FieldByName("InfoID").SetUint(uint64(info.ID))

//         // 3. 创建扩展信息
//         return tx.Create(ext).Error
//     })
// }

// // 获取信息（包含扩展数据）
// func (dao *informationDAO) GetWithExtensions(ctx context.Context, id uint) (*Information, error) {
//     var info Information
//     if err := dao.db.WithContext(ctx).First(&info, id).Error; err != nil {
//         return nil, err
//     }

//     // 动态加载扩展数据
//     switch info.InfoType {
//     case "agency_transfer":
//         var ext AgencyTransfer
//         dao.db.First(&ext, "info_id = ?", info.ID)
//         info.AgencyTransfer = &ext
//     case "equipment_transfer":
//         var ext EquipmentCommon
//         dao.db.First(&ext, "info_id = ?", info.ID)
//         info.EquipmentCommon = &ext
//     // 其他类型处理...
//     }

//     return &info, nil
// }
